package com.jackrain.nea.fc.cp.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户档案服务接口Mapper层
 *
 * @author wuwenxue
 * @date 2017/11/07
 **/
@Mapper
public interface ViewCusersMapper {

    /**
     * 外键关联：店仓
     *
     * @param id
     * @return
     */
    @Select("select ECODE from CP_C_STORE where ID=#{id}")
    String findByStoreID(Long id);

    /**
     * 外键关联：配销中心
     *
     * @param id
     * @return
     */
    @Select("select ECODE from CP_C_STOREORG where ID=#{id}")
    String findByDistribID(Long id);

    /**
     * 查询用户表USERS
     *
     * @param id
     * @return String
     */
    @Select("select ENAME from USERS where ID=#{id}")
    String findByUserId(Long id);

    @Select("select ENAME FROM CP_C_HRORG WHERE ID=#{id}")
    String findByOrg(Long id);

    /**
     * 根据传入json条件查询主表记录
     *
     * @param id
     * @return List
     */
    @SelectProvider(type = ViewCusersSql.class, method = "findCusers")
    List<HashMap> findAllUsers(@Param("id") Object[] id);

    class ViewCusersSql {
        public String findCusers(Map<String, Object> map) {
            Object[] id = (Object[]) map.get("id");
            return new SQL() {
                {
                    SELECT("NAME,ENAME,CP_C_STORE_ID,CP_C_DISTRIB_ID,MOBIL,EMAIL,REMARK,ISENABLED,MODIFIERID," +
                            "MODIFIEDDATE,PASSWORD,CP_C_HRORG_ID,SEX,F4,RF,PB1,PB2,PB3,PB4,PB5,PB6,DISCOUNTLIMIT");
                    FROM("USERS");
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < id.length; i++) {
                        sb.append(id[i] + ",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    WHERE("ID IN (" + sb.toString() + ")");
                    WHERE("USERSTYPE='IN'");
                }
            }.toString();
        }
    }
}
